/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package WebSvc;

import Helper.Helper_Area;
import Helper.Helper_Outlet;
import Helper.Helper_Stakeholder_Food;
import Helper.Helper_Timeslot;
import entity.FoodItem;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.MatrixParam;
import javax.ws.rs.Produces;
import session.TX.AreaSB;
import session.TX.AreaSBRemote;
import session.TX.CustomerReservationSB;
import session.TX.CustomerReservationSBRemote;
import session.TX.StakeholderAccountSB;
import session.TX.StakeholderAccountSBRemote;
import session.TX.TimeslotSBRemote;
import session.TX.TimeslotSB;

/**
 * REST Web Service
 *
 * @author Vic
 */
@Stateless
@Path("mobileTxSvc")
public class mobileTxSvc {

    @Context
    private UriInfo context;

    /** Creates a new instance of FoodSageWebSvc */
    public mobileTxSvc() {
    }
    @EJB
    private StakeholderAccountSBRemote rsearchSB;
    //private ReservationSearchMB rsearchSB = new ReservationSearchMB();
    @EJB
    private AreaSBRemote areaSB;
    @EJB
    private TimeslotSBRemote timeSB;
    @EJB
    private CustomerReservationSBRemote resSB;

    @GET
    @Produces("application/xml")
    @Path("getAllStakeholders")
    public String getAllStakeholders() {

        // GET ALL AVAILABLE STAKEHOLDERS FOR RESERVATIONS

        List<Helper_Stakeholder_Food> sh = getRsearchSB().getAllStakeholderFoodHelper();
        String output = "<Stakeholders>";
        for (Helper_Stakeholder_Food s : sh) {
            String tempOutput = injectElement("Name", s.getName())
                    + injectElement("OutletsNum", s.getOutlet().size());
            output += injectElement("Stakeholder", tempOutput);
        }
        output += "</Stakeholders>";
        return output;
    }

    @GET
    @Produces("application/xml")
    @Path("getStakeholderOutlets")
    public String getStakeholderOutlets(@MatrixParam("Stakeholder") String query) {

        // GET A STAKEHOLDER'S OUTLETS
        Helper_Stakeholder_Food sh = null;
        List<Helper_Stakeholder_Food> shList = getRsearchSB().getAllStakeholderFoodHelper();
        if (shList == null) {
            return "<Stakeholders><Error>-1 - No stakeholders List found</Error></Stakeholder>";
        }
        for (Helper_Stakeholder_Food s : shList) {
            if (s.getName().toLowerCase().contains(query.toLowerCase())) {
                sh = s; //get first match
                break;
            }
        }

        if (sh == null) {
            return "<Stakeholder><Error>-1 - No stakeholder found</Error></Stakeholder>";
        }

        List<Helper_Outlet> sho = sh.getOutlet();
        String output = "<Stakeholder>";
        output += injectElement("Name", sh.getName())
                + "<Outlets>";
        for (Helper_Outlet o : sho) {
            String tempOutput = injectElement("Id", o.getId())
                    + injectElement("Name", o.getOutletName())
                    + injectElement("Address", o.getAddress())
                    + injectElement("PhoneNum", o.getPhoneNumber())
                    + injectElement("LeadTime", o.getLeadTime_Day())
                    + injectElement("BookingWindow", o.getBookingWindow());
            tempOutput += "<Cuisines>";
            for (String c : o.getCuisineNames()) {
                tempOutput += injectElement("Cuisine", c);
            }
            tempOutput += "</Cuisines>";

//            tempOutput += "<FoodItems>";
//            for (FoodItem f : o.getFoodMenu()) {
//                if (f.getLocked() != true) { // FoodItem is not locked by T-cube admin
//                    tempOutput += "<Item>"
//                            + injectElement("id", f.getId())
//                            + injectElement("Name", f.getName())
//                            + injectElement("Description", f.getDescription())
//                            + injectElement("Price", f.getPrice())
//                            + injectElement("Type", f.getType()) //Promotional, etc
//                            + injectElement("ImgUrl", f.getPathName())
//                            + "</Item>";
//                }
//            }
//            tempOutput += "</FoodItems>";

            output += injectElement("Outlet", tempOutput);
        }
        output += "</Outlets>"
                + "</Stakeholder>";
        return output;
    }

    @GET
    @Produces("application/xml")
    @Path("getOutletAreas")
    public String getOutletAreas(@MatrixParam("Outlet") Long query) {

        List<Helper_Area> ah = getAreaSB().getAllHelperAreas(query);
        Helper_Area area = new Helper_Area();
        String output = "<Areas>";
        for (Helper_Area o : ah) {
            String tempOutput =
                    injectElement("AreaId", o.getId())
                    + injectElement("Name", o.getAreaName())
                    + injectElement("PicPath", o.getPathName());
            for (Integer num : o.getNumDiners()) {
                tempOutput += injectElement("Diner", num);
            }

            output += injectElement("Area", tempOutput);
        }

        output += "</Areas>";

        // List<Helper_Timeslot> tsList = timeSB.getHelperTimeslot(query, query)

        return output;
    }

    @GET
    @Produces("application/xml")
    @Path("getTimeslotList")
    public String getTimeslotList(@MatrixParam("Outlet") Long query, @MatrixParam("Area") Long query1) {

        List<Helper_Timeslot> tsList = getTimeSB().getHelperTimeslot(query, query1);

        String output = "<Timeslots>";

        if (tsList == null) {
            return output + "</Timeslots>";
        }

        for (Helper_Timeslot o : tsList) {
            String tempOutput =
                    injectElement("TimeslotId", o.getId())
                    + injectElement("Time", o.getTsTime());

            output += injectElement("Timeslot", tempOutput);
        }

        output += "</Timeslots>";

        // List<Helper_Timeslot> tsList = timeSB.getHelperTimeslot(query, query)

        return output;
    }

    @GET
    @Produces("application/xml")
    @Path("makeReservation")
    public String makeReservation(@MatrixParam("Email") String email, @MatrixParam("Outlet") Long id, @MatrixParam("Date") Long epoch,
            @MatrixParam("Timeslot") Integer tsTime, @MatrixParam("Area") String areaName, @MatrixParam("Diners") Integer diner, @MatrixParam("Comments") String comments) {

        Date date = new Date();
        date.setTime(epoch * 1000);

        Calendar cdate = Calendar.getInstance();
        cdate.setTime(date);

        Long res = getResSB().AddUnconfirmedReservation(email, id, cdate, tsTime, areaName, comments, diner);

        String output = injectElement("Error", res);

        return output;
    }

    // METHOD STATUS: LIVE
    private String injectEmptyElement(String tagName) {
        return "<" + tagName + "></" + tagName + ">";
    }

    // METHOD STATUS: LIVE
    private String injectElement(String elementName, Object content) {
        return "<" + elementName + ">" + content + "</" + elementName + ">";
    }

    // METHOD STATUS: LIVE
    private String parseOutput(Long l) {
        if (l == null) {
            return "";
        }
        return l.toString();
    }

    // METHOD STATUS: LIVE
    private String parseOutput(String input) {
        if (input == null) {
            return "";
        }
        if (input.equals("null")) {
            return "";
        }
        return input;
    }

    /**
     * PUT method for updating or creating an instance of FoodSageWebSvc
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes("application/xml")
    public void putXml(String content) {
    }

    /**
     * @return the context
     */
    public UriInfo getContext() {
        return context;
    }

    /**
     * @param context the context to set
     */
    public void setContext(UriInfo context) {
        this.context = context;
    }

    /**
     * @return the rsearchSB
     */
    public StakeholderAccountSBRemote getRsearchSB() {
        return rsearchSB;
    }

    /**
     * @param rsearchSB the rsearchSB to set
     */
    public void setRsearchSB(StakeholderAccountSBRemote rsearchSB) {
        this.rsearchSB = rsearchSB;
    }

    /**
     * @return the areaSB
     */
    public AreaSBRemote getAreaSB() {
        return areaSB;
    }

    /**
     * @param areaSB the areaSB to set
     */
    public void setAreaSB(AreaSBRemote areaSB) {
        this.areaSB = areaSB;
    }

    /**
     * @return the timeSB
     */
    public TimeslotSBRemote getTimeSB() {
        return timeSB;
    }

    /**
     * @param timeSB the timeSB to set
     */
    public void setTimeSB(TimeslotSBRemote timeSB) {
        this.timeSB = timeSB;
    }

    /**
     * @return the resSB
     */
    public CustomerReservationSBRemote getResSB() {
        return resSB;
    }

    /**
     * @param resSB the resSB to set
     */
    public void setResSB(CustomerReservationSBRemote resSB) {
        this.resSB = resSB;
    }
}
